package test.common;

import com.alibaba.fastjson.annotation.JSONType;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @author jack
 * @desc
 * @date 2022/3/15 9:18
 */
@JSONType(orders={"transitionId","signatureAlg","resultCode","resultMsg"})
public class RespHead {
    /**业务编号 */
    private String transitionId;
    /**签名算法 */
    private String signatureAlg;
    /**错误码 */
    private Integer resultCode;
    /**错误描述 */
    private String resultMsg;

    public RespHead() {
    }

    public RespHead(String transitionId, String signatureAlg, Integer resultCode, String resultMsg) {
        this.transitionId = transitionId;
        this.signatureAlg = signatureAlg;
        this.resultCode = resultCode;
        this.resultMsg = resultMsg;
    }

    public String getTransitionId() {
        return transitionId;
    }

    public void setTransitionId(String transitionId) {
        this.transitionId = transitionId;
    }

    public String getSignatureAlg() {
        return signatureAlg;
    }

    public void setSignatureAlg(String signatureAlg) {
        this.signatureAlg = signatureAlg;
    }

    public Integer getResultCode() {
        return resultCode;
    }

    public void setResultCode(Integer resultCode) {
        this.resultCode = resultCode;
    }

    public String getResultMsg() {
        return resultMsg;
    }

    public void setResultMsg(String resultMsg) {
        this.resultMsg = resultMsg;
    }

    /**
     * 返回时间戳序列号
     *
     * @return
     */
    public static String getSerNo() throws NoSuchAlgorithmException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = simpleDateFormat.format(new Date());
        String s = "";
        s += format;
        s += randomNum(6);
        return s;
    }

    /**
     * 生成随机数
     *
     * @param len
     * @return 返回字符串
     */
    private static String randomNum(int len) throws NoSuchAlgorithmException {
        Random random = SecureRandom.getInstanceStrong();
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < len; i++) {
            stringBuffer.append(random.nextInt(10));
        }
        return stringBuffer.toString();
    }

    /**
     * 创建返回成功对象</br>
     *
     * @param transitionId    错误代码</br>
     * @param signatureAlg    错误信息</br>
     * @param resultCode      错误代码</br>
     * @param resultMsg       错误信息</br>
     */
    public static <T> RespHead error(String transitionId, String signatureAlg,Integer resultCode,String resultMsg) {
        return new RespHead(transitionId, signatureAlg, resultCode, resultMsg);
    }
}
